package com.ly.quickdev.library.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ly.quickdev.library.R;
import com.ly.quickdev.library.adapter.ListBaseAdapter;

/**
 * 列表加载通用fragment
 * 如果需要一个自定义布局文件，请保持自定义的布局文件中，分别有R.id.loading,R.id.list,R.id.empty这3个id就可以
 * 
 * @author 李洋
 * 
 * @param <T>
 */
public abstract class DevBaseListFragment<T extends Parcelable> extends
DevBaseFragment implements OnItemClickListener,
OnRefreshListener2<ListView> {

	private static final String KEY_DATA = "data";
	private static final String KEY_PAGE_NO = "page";
	/** 加载的数据 */
	public List<T> mList;

	/** 当前页码 */
	public int pageNo;
	/** 数据适配器 */
	public ListBaseAdapter<T> mAdapter;

	public PullToRefreshListView mListView;

	View mView;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mView = inflater.inflate(R.layout.fragment_list, null);
		mListView = (PullToRefreshListView) mView.findViewById(R.id.list);

		return mView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		mListView = (PullToRefreshListView) view.findViewById(R.id.list);
		mListView.setOnRefreshListener(this);
		mListView.setOnItemClickListener(this);
		/** 此处可扩展，可以修改为能自定义刷新模式 */
		mListView.setMode(getRefreshMode());
	}

	public void setEmptyMessage(CharSequence msg) {
	}

	/**
	 * 默认返回上下都可以刷新
	 * 
	 * @return
	 */
	public Mode getRefreshMode() {
		return Mode.BOTH;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Log.i("tagtest", "onActivityCreatedBefore");
		if (savedInstanceState != null) {
			mList = savedInstanceState.getParcelableArrayList(KEY_DATA);
			pageNo = savedInstanceState.getInt(KEY_PAGE_NO);
		}

		if (mList == null || mList.isEmpty()) {
			pageNo = 0;
			loadData();
		} else {
			Log.i("tagtest", "onActivityCreatedAfter");

			mAdapter = newAdapter();
			mListView.setAdapter(mAdapter);
			showList();
		}
	}

	/**
	 * 此方法只可用于onItemClick回调中
	 * 
	 * @param position
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T getItem(int position) {
		return (T) mListView.getRefreshableView().getAdapter()
				.getItem(position);
	}

	/**
	 * 显示列表
	 */
	public void showList() {
		
		Log.i("tagtest", "showlist");
		mListView.setVisibility(View.VISIBLE);
	}

	/**
	 * 返回一个新的adapter
	 * 
	 * @return
	 */
	public ListBaseAdapter<T> newAdapter() {
		return new ListBaseAdapter<T>(getActivity(), mList) {

			@Override
			public View initView(int position, View convertView,
					ViewGroup parent) {
				// TODO Auto-generated method stub
				return DevBaseListFragment.this.getView(position, convertView,
						parent);
			}

			@Override
			public int getViewTypeCount() {
				// TODO Auto-generated method stub
				return DevBaseListFragment.this.getViewTypeCount();
			}

			@Override
			public int getItemViewType(int position) {
				// TODO Auto-generated method stub
				return DevBaseListFragment.this.getItemViewType(position);
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return DevBaseListFragment.this.getCount();
			}
		};
	}

	public int getCount() {
		return mList.size();
	}

	/**
	 * 默认列表中只有一种convertView 如果列表中有不同的view类型，需重写此方法
	 * 
	 * @return
	 */
	public int getViewTypeCount() {
		return 1;
	}

	/**
	 * 默认convertView 类型是0 如果列表中有不同的view类型，需重写此方法
	 * 
	 * @param position
	 * @return
	 */
	public int getItemViewType(int position) {
		return 0;
	}

	/**
	 * 数据改变后设置adapter
	 */
	public void setListAdapter() {
		Log.i("tagtest", "setListAdapterBefore");
		if (mAdapter == null) {
			mAdapter = newAdapter();
			try {
				mListView.setAdapter(mAdapter);
			} catch(NullPointerException e) {
				mAdapter = newAdapter();
				mListView.setAdapter(mAdapter);	
			}
		} else {
			mAdapter.notifyDataSetChanged();
		}
		Log.i("tagtest", "setListAdapterAfter");
		showList();
	}

	/**
	 * 保存数据
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);

		if (!isEmpty(mList)) {
			outState.putParcelableArrayList(KEY_DATA,
					(ArrayList<? extends Parcelable>) mList);
		}
		outState.putInt(KEY_PAGE_NO, pageNo);
	}

	/**
	 * 判断列表是否为空
	 * 
	 * @param list
	 * @return
	 */
	public static <T> boolean isEmpty(List<T> list) {
		return list == null || list.isEmpty();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		pageNo = 0;
		loadData();
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		pageNo++;
		loadData();
	}

	/**
	 * 此方法返回的是{@link android.widget.BaseAdapter#getView(int, View, ViewGroup)}
	 * 列表通过此方法加载每一行的item应该显示的view
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 * @return
	 */
	public abstract View getView(int position, View convertView,
			ViewGroup parent);

	/**
	 * 加载数据的方法
	 */
	public abstract void loadData();

	/**
	 * 每一行点击事件
	 */
	@Override
	public abstract void onItemClick(AdapterView<?> parent, View view,
			int position, long id);
}
